//
//  ViewController.m
//  PlayDemo
//
//  Created by Topsky on 2017/5/18.
//  Copyright © 2017年 Topsky. All rights reserved.
//

#import "ViewController.h"
#import <IJKMediaFramework/IJKMediaFramework.h>
#import "Masonry.h"

@interface ViewController ()
{
    UIButton *playBtn;
}
@property (atomic, retain) id <IJKMediaPlayback> player;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSURL *url = [NSURL URLWithString:@"rtmp://192.168.0.103:1935/rtmplive/room"];
    IJKFFOptions *options = [IJKFFOptions optionsByDefault];
    //解码参数，画面更清晰
    [options setCodecOptionIntValue:IJK_AVDISCARD_DEFAULT forKey:@"skip_loop_filter"];
    //
    [options setCodecOptionIntValue:IJK_AVDISCARD_DEFAULT forKey:@"skip_frame"];
    
     _player = [[IJKFFMoviePlayerController alloc] initWithContentURL:url withOptions:options];
    UIView *videoView = [self.player view];
    videoView.backgroundColor = [UIColor blackColor];
    UITapGestureRecognizer *tapGestureRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
    [videoView addGestureRecognizer:tapGestureRecognizer];
    [self.view addSubview:videoView];
    [videoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    playBtn = [UIButton new];
    [playBtn setBackgroundColor:[UIColor whiteColor]];
    [playBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [playBtn setTitle:@"暂停" forState:UIControlStateNormal];
    [playBtn addTarget:self action:@selector(playDidTouch) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:playBtn];
    [playBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.equalTo(@40);
    }];
    
    [_player setScalingMode:IJKMPMovieScalingModeAspectFit];
    
    [self installMovieNotificationObservers];
    [self.player prepareToPlay];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        playBtn.hidden = YES;
    });
}

//播放、暂停
- (void)playDidTouch{
    if (![self.player isPlaying]) {
        [self.player play];
        [playBtn setTitle:@"暂停" forState:UIControlStateNormal];
    }else{
        [self.player pause];
        [playBtn setTitle:@"播放" forState:UIControlStateNormal];
    }
}

- (void)handleTap:(UITapGestureRecognizer *)sender {
    if (sender.state == UIGestureRecognizerStateEnded){
        playBtn.hidden = !playBtn.hidden;
    }
}

#pragma mark-----Selector func

- (void)loadStateDidChange:(NSNotification*)notification {
    IJKMPMovieLoadState loadState = _player.loadState;
    
    if ((loadState & IJKMPMovieLoadStatePlaythroughOK) != 0) {
        NSLog(@"111-LoadStateDidChange: IJKMovieLoadStatePlayThroughOK: %d\n",(int)loadState);
    }else if ((loadState & IJKMPMovieLoadStateStalled) != 0) {
        NSLog(@"111-loadStateDidChange: IJKMPMovieLoadStateStalled: %d\n", (int)loadState);
    } else {
        NSLog(@"111-loadStateDidChange: ???: %d\n", (int)loadState);
    }
}

- (void)moviePlayBackFinish:(NSNotification*)notification {
    
    int reason =[[[notification userInfo] valueForKey:IJKMPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
    switch (reason) {
        case IJKMPMovieFinishReasonPlaybackEnded:
            NSLog(@"222-playbackStateDidChange: IJKMPMovieFinishReasonPlaybackEnded: %d\n", reason);
            break;
            
        case IJKMPMovieFinishReasonUserExited:
            NSLog(@"222-playbackStateDidChange: IJKMPMovieFinishReasonUserExited: %d\n", reason);
            break;
            
        case IJKMPMovieFinishReasonPlaybackError:
            NSLog(@"222-playbackStateDidChange: IJKMPMovieFinishReasonPlaybackError: %d\n", reason);
            break;
            
        default:
            NSLog(@"222-playbackPlayBackDidFinish: ???: %d\n", reason);
            break;
    }
}

- (void)mediaIsPreparedToPlayDidChange:(NSNotification*)notification {
    NSLog(@"333-mediaIsPrepareToPlayDidChange\n");
}

- (void)moviePlayBackStateDidChange:(NSNotification*)notification {
    switch (_player.playbackState) {
        case IJKMPMoviePlaybackStateStopped:
            NSLog(@"444-IJKMPMoviePlayBackStateDidChange %d: stoped", (int)_player.playbackState);
            break;
            
        case IJKMPMoviePlaybackStatePlaying:
            
            NSLog(@"444-IJKMPMoviePlayBackStateDidChange %d: playing", (int)_player.playbackState);
            break;
            
        case IJKMPMoviePlaybackStatePaused:
            NSLog(@"444-IJKMPMoviePlayBackStateDidChange %d: paused", (int)_player.playbackState);
            break;
            
        case IJKMPMoviePlaybackStateInterrupted:
            NSLog(@"444-IJKMPMoviePlayBackStateDidChange %d: interrupted", (int)_player.playbackState);
            break;
            
        case IJKMPMoviePlaybackStateSeekingForward:
        case IJKMPMoviePlaybackStateSeekingBackward: {
            NSLog(@"444-IJKMPMoviePlayBackStateDidChange %d: seeking", (int)_player.playbackState);
            break;
        }
            
        default: {
            NSLog(@"444-IJKMPMoviePlayBackStateDidChange %d: unknown", (int)_player.playbackState);
            break;
        }
    }
}

#pragma mark----- Install Notifiacation

- (void)installMovieNotificationObservers {
    [[NSNotificationCenter defaultCenter] addObserver:self
                     selector:@selector(loadStateDidChange:)
                         name:IJKMPMoviePlayerLoadStateDidChangeNotification
                       object:_player];
    [[NSNotificationCenter defaultCenter] addObserver:self
                     selector:@selector(moviePlayBackFinish:)
                         name:IJKMPMoviePlayerPlaybackDidFinishNotification
                       object:_player];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                     selector:@selector(mediaIsPreparedToPlayDidChange:)
                         name:IJKMPMediaPlaybackIsPreparedToPlayDidChangeNotification
                       object:_player];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                     selector:@selector(moviePlayBackStateDidChange:)
                         name:IJKMPMoviePlayerPlaybackStateDidChangeNotification
                       object:_player];
    
}

- (void)removeMovieNotificationObservers {
    [[NSNotificationCenter defaultCenter] removeObserver:self
                            name:IJKMPMoviePlayerLoadStateDidChangeNotification
                          object:_player];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                            name:IJKMPMoviePlayerPlaybackDidFinishNotification
                          object:_player];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                            name:IJKMPMediaPlaybackIsPreparedToPlayDidChangeNotification
                          object:_player];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                            name:IJKMPMoviePlayerPlaybackStateDidChangeNotification
                          object:_player];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
